package com.zj.web.controller;

import com.zj.service.AddressService;
import com.zj.web.bean.AddressDTO;
import com.zj.web.bean.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/auth/address")
public class AddressController {
    private final AddressService addressService;
    public AddressController(AddressService addressService) {
        this.addressService = addressService;
    }

    //添加地址
    @PostMapping("/add")
    @Operation(summary = "添加地址",description = "根据用户id添加地址")
    public ResponseEntity<ResponseResult> addAddress(@RequestParam("userId") Long userId,
                                                     @RequestParam("recipientName") String recipientName,
                                                     @RequestParam("phone") String phone,
                                                     @RequestParam("detailedAddress") String detailedAddress){
        AddressDTO addressDTO=new AddressDTO();
        addressDTO.setUserId(userId);
        addressDTO.setRecipientName(recipientName);
        addressDTO.setPhone(phone);
        addressDTO.setDetailedAddress(detailedAddress);
        addressService.addAddress(addressDTO);
        return ResponseEntity.ok(ResponseResult.builder().code(1).msg("添加成功").data(addressDTO).build());
    }

    //根据状态查询地址
    @GetMapping("/getAddressesByStatus/{addressStatus}/{userId}")
    @Operation(summary = "根据状态查询地址",description = "根据地址状态查询地址")
    public ResponseEntity<ResponseResult> getAddressesByStatus(@PathVariable("addressStatus") Integer addressStatus,
                                                               @PathVariable("userId") Long userId){
        List<AddressDTO> addresses = addressService.getAddressesByStatus(addressStatus,userId);
        return ResponseEntity.ok(ResponseResult.builder().code(1).msg("查询成功").data(addresses).build());
    }

    //根据地址索引更新为是否是默认地址
    @PutMapping("/updateIsDefault/{addressId}/{isDefault}/{userId}")
    @Operation(summary = "更新地址为默认地址",description = "根据地址索引更新为是否是默认地址")
    public ResponseEntity<ResponseResult> updateIsDefault(@PathVariable("addressId") Long address_id,
                                                          @PathVariable("isDefault") Integer is_default,
                                                          @PathVariable("userId") Long userId){
        boolean result = addressService.updateIsDefault(address_id, is_default,userId);
        return ResponseEntity.ok(ResponseResult.builder().code(1).msg("更新成功").data(result).build());
    }
    //根据地址索引改变地址的状态
    @PutMapping("/updateAddressStatus/{addressId}/{addressStatus}")
    @Operation(summary = "更新地址状态",description = "根据地址索引改变地址的状态")
    public ResponseEntity<ResponseResult> updateAddressStatus(@PathVariable("addressId") Long address_id,
                                                              @PathVariable("addressStatus") Integer address_status){
        boolean result = addressService.updateAddressStatus(address_id, address_status);
        return ResponseEntity.ok(ResponseResult.builder().code(1).msg("修改成功").data(result).build());
    }
    //根据地址索引修改地址信息
    @PutMapping("/updateAddress/{addressId}")
    @Operation(summary = "根据索引修改地址信息",description = "根据地址索引修改地址信息")
    public ResponseEntity<ResponseResult> updateAddress(@PathVariable("addressId") Long address_id,
                                                        @RequestParam("recipientName") String recipient_name,
                                                        @RequestParam("phone") String phone,
                                                        @RequestParam("detailedAddress") String detailed_address){
        AddressDTO addressDTO=new AddressDTO();
        addressDTO.setAddressId(address_id);
        addressDTO.setRecipientName(recipient_name);
        addressDTO.setPhone(phone);
        addressDTO.setDetailedAddress(detailed_address);
        boolean result = addressService.updateAddress(address_id,addressDTO);
        return ResponseEntity.ok(ResponseResult.builder().code(1).msg("修改成功").data(result).build());
    }

    //查找默认地址的地址信息
    @Operation(summary = "查找默认地址的地址信息",description = "根据是否是默认地址查找默认地址的地址信息")
    @GetMapping("/getDefaultAddress/{isDefault}/{userId}")
    public ResponseEntity<ResponseResult> getDefaultAddress(@PathVariable("isDefault") Integer is_default,
                                                           @PathVariable("userId") Long userId){
        AddressDTO address = addressService.getDefaultAddress(is_default,AddressService.ADDRESS_STATUS_ENABLE,userId);
        if(address==null){
            return ResponseEntity.ok(ResponseResult.builder().code(0).msg("查询失败").data(null).build());
        }
        return ResponseEntity.ok(ResponseResult.builder().code(1).msg("查询成功").data(address).build());
    }

}
